﻿using System;
using System.Collections.Generic;
using System.Text;
using Tao.OpenGl;
using Tao.FreeGlut;
using Tao.DevIl;
using Tao.Sdl;
using System.IO;

namespace AADD
{

    public class main
    {
        const int SCREEN_W = 800;
        const int SCREEN_H = 600;
        public static string RESOURCEDIRECTORY = "../../../Resources/";
        public static int TILESIZE = 32;
        public static int buttonMask = 0;
        public static int buttonX, buttonY;

        public static int cButtonID;  // circle button
        public static int eButtonID;  // ellipse button
        public static int cFButtonID; // filled circle
        public static int eFButtonID; // filled ellipes

        public static SpritesheetManager spriteManager;

        private static void displayControllerInterface()
        {
            const int width = 384;
            const int height = 160;
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glVertex2i(0, 0);
            Gl.glVertex2i(width, 0);
            Gl.glVertex2i(width, height);
            Gl.glVertex2i(0, height);
            Gl.glEnd();


            Gl.glBindTexture(Gl.GL_TEXTURE_2D, cButtonID);
            if ((buttonMask & 1) != 0)
            {

            Gl.glBindTexture(Gl.GL_TEXTURE_2D, cFButtonID);
            }

            Gl.glPushMatrix();
                Gl.glBegin(Gl.GL_QUADS);
                Gl.glTexCoord2d(0, 0);
                Gl.glVertex2i(0, 0);
                Gl.glTexCoord2d(1, 0);
                Gl.glVertex2i(width, 0);
                Gl.glTexCoord2d(1, 1);
                Gl.glVertex2i(width, height);
                Gl.glTexCoord2d(0, 1);
                Gl.glVertex2i(0, height);
                Gl.glEnd();

            Gl.glBindTexture(Gl.GL_TEXTURE_2D, eButtonID);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, eFButtonID);


        }

        private static void Display()
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);

            Gl.glLoadIdentity();

            //displayControllerInterface();



            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    spriteManager.blit("test.tga", i * 4 + j);
                    Gl.glTranslatef(32, 0, 0);
                }

                Gl.glTranslatef(-32*4, 32, 0);
            }

            Glut.glutSwapBuffers();
        }

        private static void KeyPressed(byte key, int mx, int my)
        {
            char cKey = (char)key;
            switch (cKey)
            {

                case (char)27: // escape
                case 'q':
                    Environment.Exit(0);
                    break;
            }
        }

        private static void Idle()
        {
            Glut.glutPostRedisplay();
        }

        private static void Joystick(int buttonsHeld, int x, int y, int z)
        {
            buttonMask = buttonsHeld;
            buttonX = x;
            buttonY = y;
            //Console.WriteLine("{0} {1} {2} {3}", buttonMask, x, y, z);
        }

        public static void Main(String[] args)
        {

            // Initialize GLUT library
            Glut.glutInit();
            Il.ilInit();
            // Initialize the window settings
            Glut.glutInitDisplayMode(Glut.GLUT_RGB | Glut.GLUT_DOUBLE);
            Glut.glutInitWindowSize(SCREEN_W, SCREEN_H);
            Glut.glutCreateWindow("AADD - Badam and Sandshrew");
            Gl.glEnable(Gl.GL_TEXTURE_2D);
            Init();


            // Set the callback functions
            Glut.glutDisplayFunc(new Glut.DisplayCallback(Display));
            Glut.glutKeyboardFunc(new Glut.KeyboardCallback(KeyPressed));
            Glut.glutJoystickFunc(new Glut.JoystickCallback(Joystick), 50);
            Glut.glutIdleFunc(new Glut.IdleCallback(Idle));

            // Enter the main loop. Glut will be here permanently from now on
            // until we quit the program
            Glut.glutMainLoop();
        }

        private static void Init()
        {
            spriteManager = new SpritesheetManager();


            Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);


            Gl.glMatrixMode(Gl.GL_PROJECTION);						// Select The Projection Matrix
            Gl.glLoadIdentity();									// Reset The Projection Matrix

            //Gl.glViewport(0, 0, SCREEN_W, SCREEN_H);
            Glu.gluOrtho2D(0, SCREEN_W, SCREEN_H, 0);

            Gl.glMatrixMode(Gl.GL_MODELVIEW);							// Select The Modelview Matrix
            Gl.glLoadIdentity();									// Reset The Modelview Matrix

            string inputFile = RESOURCEDIRECTORY + "test2.tga";
            if (!File.Exists(inputFile))
            {
                throw new Exception(string.Format("Could not open file, {0}, exiting.", inputFile));
            }

        }


    }
}
